Encrypts or decrypts a text file

================================================

/*
* cipher.cpp
*
* Written byGraham Watt
*/

#include <fstream>
#include <string>

using namespace std;

/*
* Use by passing the file name and password as strings
*/
string encode(const string fileName, const string passwd)
{
    /*
     * I don't want to replace the file
     * so I'm making a copy
    */
    string encFileName = fileName + ".enc";
    string passwdCopy(passwd);
    ifstream file(fileName.c_str() );
    ofstream encFile(encFileName.c_str() );
    int index = 0;
    /*
     * For encrypting the file I use a polyalphabetic cipher.
     * I take add a character in the password to a character
     * in the file.  When I reach the last element of the
     * password I start over from the beginning.
    */
    char c = file.get();
    while(!file.eof() )
    {
        encFile.put(c + passwdCopy[index]++);
        index++;
        index %= passwdCopy.length();
        c = file.get();
    }
    file.close();
    encFile.close();
    return encFileName;
}

string decode(const string encFileName, const string passwd)
{
    string fileName = encFileName + ".clear";
    string passwdCopy(passwd);
    ifstream encFile(encFileName.c_str() );
    ofstream file(fileName.c_str() );
    int index = 0;
    /*
     * decryption is pretty much the same as encryption
     * except that I subtract the password character
    */
    char c = encFile.get();
    while(!encFile.eof() )
    {
        file.put(c - passwdCopy[index]++);
        index++;
        index %= passwdCopy.length();
        c = encFile.get();
    }
    file.close();
    encFile.close();
    return fileName;
}



===============================================


Pass the filename and password as strings to each function. 